﻿using Kevin.Shop.Business.Service.Entity.Helper;
using Kevin.Shop.Business.Service.Entity.SalesManagement.PromotionCode;
using RiderShop.Mobile.Common.Component;
using RiderShop.Mobile.Common.Component.OAuth.Weixing;
using RiderShop.Mobile.Modle.MyAccount;
using RiderShop.Mobile.ServiceEntity.MyAccount;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace RiderShop.Mobile.Controllers
{

    public partial class PromotionController : ThemeController
    {
        /// <summary>
        /// 随机得到一个游戏
        /// </summary>
        /// <param name="orderNumber">订单号</param>
        /// <param name="gameId"></param>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public ActionResult GetGameView(string orderNumber, string gameId = "")
        {
            //必须微信环境才能玩
            WeixingOAuth.OAuth();

            if (string.IsNullOrEmpty(orderNumber))
            {
                return new EmptyResult(); ;
            }

            //随机给个游戏
            if (string.IsNullOrWhiteSpace(gameId))
            {
                var dir = System.IO.Directory.GetFiles(Server.MapPath("/Views/Promotion/Games"));
                if (dir.Length > 0)
                {
                    Random ran = new Random();
                    int randomNumber = ran.Next(0, dir.Length);
                    gameId = System.IO.Path.GetFileNameWithoutExtension(dir[randomNumber]);
                }
            }

            return View("~/Views/Promotion/Games/" + gameId + ".cshtml");
        }

        /// <summary>
        /// 玩游戏得到优惠券
        /// </summary>
        /// <param name="orderNumber">订单号</param>
        /// <param name="score">玩游戏的分数</param>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public ActionResult PlayGameGetPromotionCode(string orderNumber, int score = 0)
        {
            if (string.IsNullOrEmpty(orderNumber))
            {
                return Json("notPrize");
            }

            //check是否已经领取过优惠券
            MyPromotionCodeModel myPromotionCodeModel = new MyPromotionCodeModel();
            myPromotionCodeModel.GetUserPromotionCodeByOrderNumber(orderNumber);
            PromotionCodeCustomerEntity promotionCodeCustomer = myPromotionCodeModel.PromotionCodeList.FirstOrDefault();

            if (promotionCodeCustomer != null && !string.IsNullOrEmpty(promotionCodeCustomer.PromotionCode))
            {
                return Json("alreadyReceivedPromotionCode");
            }

            string promotionCodeScore = ConfigurationManager.AppSettings["PromotionCodeScore"];

            if (!string.IsNullOrEmpty(promotionCodeScore))
            {
                if (promotionCodeScore.Contains("-"))
                {
                    List<KeyValuePair<int, string>> scoreAndPromotionCodeList = new List<KeyValuePair<int, string>>();
                    foreach (var item in promotionCodeScore.Split(','))
                    {
                        var par = item.Split('-');
                        if (par.Length == 2)
                        {
                            scoreAndPromotionCodeList.Add(new KeyValuePair<int, string>(int.Parse(par[0]), par[1].ToString()));
                        }
                    }
                    if (scoreAndPromotionCodeList != null && scoreAndPromotionCodeList.Count() > 0)
                    {
                        scoreAndPromotionCodeList = scoreAndPromotionCodeList.OrderByDescending(s => s.Key).ToList();
                        //随机给个分数
                        if (score == 0)
                        {
                            Random ran = new Random();
                            int minScore = 0;
                            int maxScore = scoreAndPromotionCodeList[0].Key;
                            int randomScore = ran.Next(minScore, maxScore);
                            score = randomScore;
                        }

                        foreach (var scoreAndPromotionCode in scoreAndPromotionCodeList)
                        {
                            if (score >= scoreAndPromotionCode.Key)
                            {
                                return Json(scoreAndPromotionCode.Value);
                            }
                        }
                    }
                }
            }

            return Json("notPrize");
        }
    }
}
